<template>
  <panel-head :route="route" />
  <div class="form">
    <el-form :inline="true" :model="searchForm">
      <el-form-item prop="out_trade_no">
        <el-input v-model="searchForm.out_trade_no" placeholder="订单号" />
      </el-form-item>
      <el-form-item prop="out_trade_no">
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <el-table :data="tableData.list" >
    <el-table-column prop="out_trade_no" lable="订单号" width="150" fixed="left"></el-table-column>
    <el-table-column prop="hospital_name" lable="就诊医院"></el-table-column>
    <el-table-column prop="service_name" lable="陪诊服务"></el-table-column>
    <el-table-column lable="陪护师头像">
      <template #default="scope">
        <el-image style="width: 40px; height: 40px;" :src="scope.row.companion.avatar" />
      </template>
    </el-table-column>
    <el-table-column lable="陪护师手机号" width="120">
      <template #default="scope">
        {{ scope.row.companion.mobile }}
      </template>
    </el-table-column>
    <el-table-column prop="price" lable="总价"></el-table-column>
    <el-table-column prop="paidPrice" lable="已付"></el-table-column>
    <el-table-column lable="下单时间" width="120">
      <template #default="scope">
        {{ dayjs(scope.row.order_start_time).format('YYYY-MM-DD') }}
      </template>
    </el-table-column>
    <el-table-column lable="订单状态">
      <template #default="scope">
        <el-tag :type="statusMap(scope.row.trade_state)" >{{ scope.row.trade_state }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="service_state" lable="接单状态"></el-table-column>
    <el-table-column prop="tel" lable="联系人手机号" width="120"></el-table-column>
    <el-table-column lable="操作" width="100" fixed="right">
      <el-popconfirm
        v-if="scope.row.trade_state === '待服务'"
        confirm-button-text="是"
        cancel-button-text="否"
        :icon="InfoFilled"
        icon-color="#626AEF"
        title="是否确认完成？"
        @confirm="confirmEvent(scope.row.out_trade_no)"
      >
        <template #reference>
          <el-button type="primary" link>服务完成</el-button>
        </template>
      </el-popconfirm>
      <el-button v-else type="primary" link disabled>暂无服务</el-button>
    </el-table-column>
  </el-table>
</template>
<script setup>
import { adminOrder, updateOrder } from '../../../api';
import { onMounted, reactive } from 'vue';
import { stringifyQuery, useRoute } from 'vue-router';
import dayjs from 'dayjs';
import { List, Plus, InfoFilled, Delete } from '@element-plus/icons-vue';


const route = useRoute();

onMounted(() => {
  getListData();
});

const paginationData = reactive({
  pageNum: 1,
  pageSize: 10,
});

const searchForm = reactive({
  out_trade_no: ''
});

const onSubmit = () => {
  getListData(searchForm);
};

const tableData = reactive({
  list: [],
  total: 0
});

const getListData = (params = {}) => {
  adminOrder({ ...paginationData, ...params }).then(({ data }) => {
    const { list, total } = data.data;
    tableData.list = list;
    tableData.total = total;
  });
};

const statusMap = (key) => {
  const obj = {
    '已取消': 'info',
    '待支付': 'warning',
    '已完成': 'success'
  }
};

const confirmEvent = (id) => {
  updateOrder({ id }).then(({ data }) => {
    if (data.code === 10000) {
      getListData();
    }
  });
};

const handleSizeChange = (val) => {
	paginationData.pageSize = val;
	getListData();
};

const handleCurrentChange = (val) => {
	paginationData.pageNum = val;
	getListData();
};
</script>
<style scoped>
.form {
  display: flex;
  justify-content: flex-end;
  padding: 10px 0 10px 10px;
  background-color: #fff;
}
</style>